package lee.study.StackAndQueue;

class MyCircleQueue {
    public int[] elem;
    public int front;
    public int rear;

    public MyCircleQueue(int k) {
        this.elem = new int[k+1];//设置队列长度为k
    }

    public boolean enQueue(int value) {//入队
        if (isfull()) {
            return false;
        }
        this.elem[this.rear] = value;
        this.rear = (this.rear + 1) % this.elem.length;
        return true;
    }

    public boolean deQueue() {//出队
        if (isEmpty()) {
            return false;
        }
        this.front = (this.front + 1) % this.elem.length;
        return true;
    }

    public int front() {//找到头部元素
        if (isEmpty()) {
            return -1;
        }
        return this.elem[this.front];
    }

    public int rear() {//找到尾部元素
        if (isEmpty()) {
            return -1;
        }
        int index = this.rear == 0 ? this.elem.length - 1 : this.rear - 1;
        return this.elem[index];
    }

    private boolean isfull() {
        return (this.rear + 1) % this.elem.length == this.front;
    }

    public boolean isEmpty() {
        return this.front == this.rear;
    }
}
